﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.sprite;
using imaginecup.util;
using Imaginecup.util.input;
using Imaginecup.util.touchMan;
using Imaginecup.util.collision;
using System.Diagnostics;
using Imaginecup.gameState.water.mapMan;

namespace Imaginecup.gameState.water.controller.phone
{
    public class LeftController:ITouchProcess
    {
        ISprite m_SpriteOut;
        ISprite m_SpriteIn;

        EntityInfo infoOut;
        EntityInfo infoIn;

        float rotation;
        MapMan m_MapMan;

        public LeftController()
        {
            NormalSprite Outleft = new NormalSprite();
            Outleft.SetTextureName("water_left_navi");
            NormalSprite Inleft = new NormalSprite();
            Inleft.SetTextureName("water_left_point");

            m_SpriteOut = Outleft;
            m_SpriteIn = Inleft;

            infoOut = new EntityInfo(1, 250, 232, 231);
            infoIn = new EntityInfo(69, 317.5f, 96, 96);

            rotation = 0.0f;
        }

        public void SetMapManager(MapMan mapManager)
        {
            m_MapMan = mapManager;
        }

        public float GetRotation()
        {
            return rotation;
        }
        public void SetRotation(Vector2 touchPosition)
        {
            const float DEADZONE_RATE = 0.2f;

            Vector2 direction = infoOut.GetEntityCenter() - touchPosition;

            float rate = direction.Length() / (infoOut.mSize.X * 0.5f);

            if ((0.0f <= rate) && (1.0f >= rate))
            {
                infoIn.SetEntityCenter(touchPosition);

                if (DEADZONE_RATE <= rate)
                {
                    rotation = (float)Math.Atan2(direction.Y, direction.X);
                }
            }

            Point iPoint = new Point();
            iPoint.X = (int)direction.X;
            iPoint.Y = (int)direction.Y;
            m_MapMan.SetDirection(iPoint);
        }
        public void Update(GameTime gameTime )
        {
                
        }
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            m_SpriteOut.Draw(spriteBatch, infoOut);
            m_SpriteIn.Draw(spriteBatch, infoIn);
        }
        
        public void TouchBegan(Microsoft.Xna.Framework.Input.Touch.TouchLocation touch)
        {
            SetRotation(touch.Position);
        }

        //리펙토링해야함
        public void TouchMove(Microsoft.Xna.Framework.Input.Touch.TouchLocation touch)
        {
            Vector2 direction = infoOut.GetEntityCenter() - touch.Position;
            float rate = direction.Length() / (infoOut.mSize.X * 0.5f);
            if((0.0f <= rate) && (1.0f >= rate))
            {
                SetRotation(touch.Position);
            }
            else if(rate > 1.0f)
            {
                //화면 밖에서 움직일 때
                Vector2 editCurrentPosition = Vector2.Zero;
                Vector2 currentPosition = touch.Position;
                Vector2 lengthCalculatePosition = new Vector2(currentPosition.X - infoOut.GetEntityCenter().X, currentPosition.Y - infoOut.GetEntityCenter().Y);
                float fLength = (float)Math.Pow(Math.Abs(lengthCalculatePosition.X), 2) + (float)Math.Pow(Math.Abs(lengthCalculatePosition.Y), 2);
                fLength = (float)Math.Sqrt(fLength);
                editCurrentPosition.X = (float)Math.Abs(lengthCalculatePosition.X) * (infoOut.mSize.X / 2) / fLength;
                editCurrentPosition.Y = (float)Math.Abs(lengthCalculatePosition.Y) * (infoOut.mSize.Y / 2) / fLength;

                if (lengthCalculatePosition.X <= 0 && lengthCalculatePosition.Y <= 0)
                {
                    //왼쪽 위
                    currentPosition.X = infoOut.GetEntityCenter().X - editCurrentPosition.X;
                    currentPosition.Y = infoOut.GetEntityCenter().Y - editCurrentPosition.Y;
                }
                else if (lengthCalculatePosition.X > 0 && lengthCalculatePosition.Y <= 0)
                {
                    //오른쪽 위
                    currentPosition.X = infoOut.GetEntityCenter().X + editCurrentPosition.X;
                    currentPosition.Y = infoOut.GetEntityCenter().Y - editCurrentPosition.Y;
                }
                else if (lengthCalculatePosition.X >= 0 && lengthCalculatePosition.Y > 0)
                {
                    //오른쪽 아래
                    currentPosition.X = infoOut.GetEntityCenter().X + editCurrentPosition.X;
                    currentPosition.Y = infoOut.GetEntityCenter().Y + editCurrentPosition.Y;
                }
                else if (lengthCalculatePosition.X < 0 && lengthCalculatePosition.Y > 0)
                {
                    //왼쪽 아래
                    currentPosition.X = infoOut.GetEntityCenter().X - editCurrentPosition.X;
                    currentPosition.Y = infoOut.GetEntityCenter().Y + editCurrentPosition.Y;
                }

                infoIn.SetEntityCenter(currentPosition);

                Point iPoint = new Point();
                iPoint.X = (int)direction.X;
                iPoint.Y = (int)direction.Y;
                m_MapMan.SetDirection(iPoint);

                //배경 방향 설정
//                touchScreenState.Direction = direction;
//                returnTouchDirection = direction;                
//                 if (returnTouchDirection != Vector2.Zero)
//                 {
//                     returnOldTouchDirection = returnTouchDirection;
//                 }                
//                 Vector2 imsi = touchScreenState.Direction;
//                 returnTouchRotation = (float)Math.Atan2(imsi.Y, imsi.X);
            }
        }

        public void TouchEnd(Microsoft.Xna.Framework.Input.Touch.TouchLocation touch)
        {
            infoIn.SetEntityCenter(infoOut.GetEntityCenter());
            m_MapMan.SetDirection(Point.Zero);
        }

        public bool CheckLocation(Microsoft.Xna.Framework.Input.Touch.TouchLocation touch)
        {
            if(Collision.CollisionByPoint(infoOut,touch.Position))
            {
                return true;
            }
            return false;
        }
    }
}
